package com.xinfu.newproject.adapter.home_provider;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;

import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.HomeActivity;
import com.xinfu.newproject.activity.HouseholdApplicationsActivity;
import com.xinfu.newproject.activity.LoginActivity;
import com.xinfu.newproject.activity.ParkingActivity;
import com.xinfu.newproject.activity.ResidentCertificationActivity;
import com.xinfu.newproject.adapter.home_adapter.Indicator2Adapter;
import com.xinfu.newproject.adapter.home_adapter.YiGridViewTwoAdapter;
import com.xinfu.newproject.bean.home.bean.GetAppIconBean;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.bean.matter_repair.RepairDetailBean;
import com.xinfu.newproject.bean.matter_repair.RepairRecordBean;
import com.xinfu.newproject.bean.matter_repair.bean.RepairCountBean;
import com.xinfu.newproject.contants.Contants;
import com.xinfu.newproject.entity.NormalMultipleEntity;
import com.xinfu.newproject.ui.matter_repair.matter_repair_child.TheRepairFragment;
import com.xinfu.newproject.ui.my_community.MyCommunityActivity;
import com.xinfu.newproject.presenter.RepairRecordPresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.LOGIN;

/**
 * https://github.com/chaychan
 *
 * @author ChayChan
 * @description: Text ItemProvider
 * @date 2018/3/30  11:39
 */

public class TextItemProvider extends BaseItemProvider<NormalMultipleEntity, BaseViewHolder> implements IView.IRepairRecordView {

    ViewPager viewPager;
    LinearLayout mLlDot2;
    private List<View> mPagerList;
    /**
     * 总的页数
     */
    private int pageCount;
    /**
     * 每一页显示的个数
     */
    private int pageSize = 8;
    /**
     * 当前显示的是第几页
     */
    private int curIndex2 = 0;
    private LayoutInflater inflater;
    private YiGridViewTwoAdapter mGridViewAdapter;
    private GetAppIconBean getAppIconBean;

    private RepairRecordPresenter repairRecordPresenter = new RepairRecordPresenter(this);

    public TextItemProvider(GetAppIconBean getAppIconBean) {
        this.getAppIconBean = getAppIconBean;
    }

    @Override
    public int viewType() {
        return HomeMultipleItemRvAdapter.TYPE_TEXT;
    }

    @Override
    public int layout() {
        return R.layout.item_text_view;
    }

    @Override
    public void convert(BaseViewHolder helper, NormalMultipleEntity data, int position) {
        viewPager = helper.getView(R.id.item_text_view_vp);
        mLlDot2 = helper.getView(R.id.item_text_view_dot);
        inflater = LayoutInflater.from(mContext);
        if (getAppIconBean != null) {
            initGridView(getAppIconBean.getResult().getMenuList());
        }
        if (isLogin()) {
            repairRecordPresenter.repairCount();
        }
    }

    private void initGridView(final List<GetAppIconBean.ResultBean.MenuListBean> entityList) {

        //总的页数=总数/每页数量，并取整
        pageCount = (int) Math.ceil(entityList.size() * 1.0 / pageSize);
        //设置圆点
        if (pageCount > 1) {
            setOvalLayout2();
        }

        mPagerList = new ArrayList<View>();
        mPagerList.clear();

        for (int i = 0; i < pageCount; i++) {
            //每个页面都是inflate出一个新实例
            GridView gridView = (GridView) inflater.inflate(R.layout.gridview, viewPager, false);
            mGridViewAdapter = new YiGridViewTwoAdapter(mContext, entityList, i, pageSize);
            gridView.setAdapter(mGridViewAdapter);
            mPagerList.add(gridView);
//            ElasticityHelper.setUpOverScroll(gridView);
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    switch (getAppIconBean.getResult().getMenuList().get(position).getCode()) {
                        case "0010":
                            if (isLogin()) {
                                Bundle bundle = new Bundle();
                                bundle.putString("response", new Gson().toJson(getAppIconBean.getResult()));
                                ((HomeActivity) mContext).startIntentActivity(MyCommunityActivity.class, bundle);
                            } else {
                                ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
                            }
                            break;
                        case "0020":
                            if (isLogin()) {
//                                ((HomeActivity) mContext).startIntentActivity(PayCostActivity.class, null);
                                Bundle bundle = new Bundle();
                                bundle.putInt("type", Contants.PAYCOSTCHOOSECOMMUNITYCERTIFITION);
                                ((HomeActivity) mContext).startIntentActivity(ResidentCertificationActivity.class, bundle);
                            } else {
                                ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
                            }
                            break;
                        case "0030":
                            if (isLogin()) {
                                //入户申请
                                ((HomeActivity) mContext).startIntentActivity(HouseholdApplicationsActivity.class, null);
                            } else {
                                ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
                            }
                            break;
                        case "0040":
                            if (isLogin()) {
                                //报修维修
                                ((HomeActivity) mContext).startIntentActivity(TheRepairFragment.class, null);
                            } else {
                                ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
                            }
                            break;
                        case "0050":
                            break;
                        case "0080":
                            break;
                        case "0090":
                            break;
                        case "0100":
                            break;
                        case "0110":
                            break;
                        case "0120":
                            if (isLogin()) {
                                ((HomeActivity) mContext).startIntentActivity(ParkingActivity.class, null);
                            } else {
                                ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
                            }
                            break;
                        case "0130":
                            break;
                        case "0131":
                            break;
                        case "0170":
                            break;
                    }
                }
            });
        }
        //设置适配器
        viewPager.setAdapter(new Indicator2Adapter(mPagerList));
    }

    @Override
    public void onClick(BaseViewHolder helper, NormalMultipleEntity data, int position) {

    }

    @Override
    public boolean onLongClick(BaseViewHolder helper, NormalMultipleEntity data, int position) {
//        Toast.makeText(mContext, "longClick", Toast.LENGTH_SHORT).show();
        return true;
    }

//    @Override
//    public void onClick(View view) {
//        switch (view.getId()) {
////            物业缴费
//            case R.id.item_home_property_rl:
//                if (isLogin()) {
//                    ((HomeActivity) mContext).startIntentActivity(PayCostActivity.class, null);
//                } else {
//                    ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
//                }
//                break;
////                停车缴费
//            case R.id.item_home_stop_card_rl:
//                if (isLogin()) {
//                    ((HomeActivity) mContext).startIntentActivity(ParkingActivity.class, null);
//                } else {
//                    ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
//                }
//                break;
//            case R.id.item_home_decorate_rl:
//                if (isLogin()) {
//                    //报事报修
//                    ((HomeActivity) mContext).startIntentActivity(DecorateActivity.class, null);
//                } else {
//                    ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
//                }
//                break;
//            case R.id.item_home_household_application_rl:
//                if (isLogin()) {
//                    //入户申请
//                    ((HomeActivity) mContext).startIntentActivity(HouseholdApplicationsActivity.class, null);
//                } else {
//                    ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
//                }
//                break;
//            case R.id.item_home_complaint_feedback_rl:
//                if (isLogin()) {
//                    //投诉反馈
//                    ((HomeActivity) mContext).startIntentActivity(ComplaintsFragment.class, null);
//                } else {
//                    ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
//                }
//                break;
//            case R.id.item_home_matter_repair_rl:
//                if (isLogin()) {
//                    //报事报修
//                    ((HomeActivity) mContext).startIntentActivity(MatterAndRepairActivity.class, null);
//                } else {
//                    ((HomeActivity) mContext).startIntentActivity(LoginActivity.class, null);
//                }
//                break;
//        }
//    }


    private void setOvalLayout2() {
        mLlDot2.removeAllViews();
        for (int i = 0; i < pageCount; i++) {
            /**
             * 生成对应数量的点点
             */
            mLlDot2.addView(inflater.inflate(R.layout.dot, null));
            mLlDot2.getChildAt(i).findViewById(R.id.v_dot)
                    .setBackgroundResource(R.drawable.dot_normal_grid);
            if (i != -1) {
                mLlDot2.getChildAt(i).findViewById(R.id.dot_tv).setVisibility(View.GONE);
            }
        }
        // 默认显示第一页
        mLlDot2.getChildAt(0).findViewById(R.id.v_dot)
                .setBackgroundResource(R.drawable.dot_selected);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageSelected(int position) {

//                if (position == 0){
//                    mLlDot2.getChildAt(0)
//                            .findViewById(R.id.v_dot)
//                            .setBackgroundResource(R.drawable.dot_selected);
//                    mLlDot2.getChildAt(1)
//                            .findViewById(R.id.v_dot)
//                            .setBackgroundResource(R.drawable.dot_normal_grid);
//                } else if (position == 1){
//                    mLlDot2.getChildAt(0)
//                            .findViewById(R.id.v_dot)
//                            .setBackgroundResource(R.drawable.dot_normal_grid);
//                    mLlDot2.getChildAt(1)
//                            .findViewById(R.id.v_dot)
//                            .setBackgroundResource(R.drawable.dot_selected);
//                }
                //选中
                mLlDot2.getChildAt(position)
                        .findViewById(R.id.v_dot)
                        .setBackgroundResource(R.drawable.dot_selected);

                // 取消圆点选中
                mLlDot2.getChildAt(curIndex2)
                        .findViewById(R.id.v_dot)
                        .setBackgroundResource(R.drawable.dot_normal_grid);
                curIndex2 = position;

            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName;

    private boolean isLogin() {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(mContext, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            BToast.showText(mContext, "请先登录");
            return false;
        }

        return true;
    }

    @Override
    public String header() {
        return token;
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public String repairId() {
        return null;
    }

    @Override
    public void showToast(String content) {

    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void getData(RepairRecordBean repairRecordBean) {

    }

    @Override
    public void getData(RepairDetailBean repairDetailBean) {

    }

    @Override
    public void getData(RepairCountBean repairCountBean) {
        if (repairCountBean.getResult() != null && mGridViewAdapter != null) {
            mGridViewAdapter.addTextSize(repairCountBean.getResult());
        }
    }
}
